package com.yunhe.mybatis.service.impl;

import com.yunhe.mybatis.common.utils.SqlSessionManager;
import com.yunhe.mybatis.dao.deparment.DeparmentDao;
import com.yunhe.mybatis.dao.pay.PayDao;
import com.yunhe.mybatis.dao.user.UserDao;
import com.yunhe.mybatis.dto.Pageable;
import com.yunhe.mybatis.entity.Pay;
import com.yunhe.mybatis.entity.User;
import com.yunhe.mybatis.service.PayService;

import java.util.List;

public class PayServiceImpl implements PayService {

    @Override
    public void save(Pay pay) {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        payDao.save(pay);
    }

    @Override
    public void update(Pay pay) {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        payDao.update(pay);
    }

    @Override
    public void remove(Integer id) {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        payDao.remove(id);
    }

    @Override
    public List<Pay> findAll() {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        return payDao.findAll();
    }

    @Override
    public Pay find(Integer id) {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        return  payDao.find(id);
    }


    @Override
    public Pageable findforPage(int page, int size) {
        PayDao payDao = SqlSessionManager.getSqlSession().getMapper(PayDao.class);
        Pageable pageable = new Pageable();
        pageable.setCurrent(page);
        pageable.setSize(size);
        int count = payDao.count();

        pageable.setCount(count);
        if(count > 0 ){
            List<Pay> list = payDao.findList((page - 1) * size, size);
            pageable.setRows(list);
            int pageCount = count/size;
            pageCount = count % size == 0 ? pageCount : pageCount + 1;
            pageable.setPageCount(pageCount);
        }
        return pageable;
    }
}
